/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#include <aknviewappui.h>
#include <eikappui.h>
#include <LDIR.rsg>
#include "LDIRAbout.h"
#include "LDIRAboutView.h"
#include "LDIR.hrh"
#include <aknutils.h> 
#include <AknsDrawUtils.h>
#include <AknsBasicBackgroundControlContext.h> 
#include <eiklabel.h>
#include <stringloader.h>

/**
 * First phase of Symbian two-phase construction. Should not 
 * contain any code that could leave.
 */
CLDIRAbout::CLDIRAbout()
	{
	}
/** 
 * Destroy child controls.
 */
CLDIRAbout::~CLDIRAbout()
	{
	delete iBackground;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is not left on cleanup stack.
 * @param aRect bounding rectangle
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return initialized instance of CLDIRAbout
 */
CLDIRAbout* CLDIRAbout::NewL( const TRect& aRect, const CCoeControl* aParent,
	MEikCommandObserver* aCommandObserver )
	{
	CLDIRAbout* self = CLDIRAbout::NewLC( aRect, aParent, aCommandObserver );
	CleanupStack::Pop( self );
	return self;
	}

/**
 * Construct the control (first phase).
 *  Creates an instance and initializes it.
 *  Instance is left on cleanup stack.
 * @param aRect The rectangle for this window
 * @param aParent owning parent, or NULL
 * @param aCommandObserver command observer
 * @return new instance of CLDIRAbout
 */
CLDIRAbout* CLDIRAbout::NewLC( const TRect& aRect, const CCoeControl* aParent,
	MEikCommandObserver* aCommandObserver )
	{
	CLDIRAbout* self = new ( ELeave ) CLDIRAbout();
	CleanupStack::PushL( self );
	self->ConstructL( aRect, aParent, aCommandObserver );
	return self;
	}

/**
 * Construct the control (second phase).
 *  Creates a window to contain the controls and activates it.
 * @param aRect bounding rectangle
 * @param aCommandObserver command observer
 * @param aParent owning parent, or NULL
 */
void CLDIRAbout::ConstructL( const TRect& aRect, const CCoeControl* aParent,
	MEikCommandObserver* aCommandObserver )
	{
	if ( aParent == NULL )
		{
		CreateWindowL();
		}
	else
		{
		SetContainerWindowL( *aParent );
		}
	iFocusControl = NULL;
	iCommandObserver = aCommandObserver;
	// support themes
	iBackground = CAknsBasicBackgroundControlContext::NewL(
		KAknsIIDQsnBgAreaMain, aRect, ETrue );
	InitializeControlsL( aRect.Width() );
	SetRect( aRect );
	ActivateL();
	}

/**
 *    Handle resizing of the container. This implementation will lay out
 *  full-sized controls like list boxes for any screen size, and will layout
 *  labels, editors, etc. to the size they were given in the UI designer.
 *  This code will need to be modified to adjust arbitrary controls to
 *  any screen size.
 */
void CLDIRAbout::SizeChanged()
	{
	CCoeControl::SizeChanged();
	LayoutControls();
	}

TTypeUid::Ptr CLDIRAbout::MopSupplyObject( TTypeUid aId )
	{
	// support themes
	return iBackground != NULL ? MAknsControlContext::SupplyMopObject( aId,
		iBackground ) : CCoeControl::MopSupplyObject( aId );
	}

/**
 * Layout components as specified in the UI Designer
 */
void CLDIRAbout::LayoutControls()
	{
	TInt width = Rect().Width();
	TInt height = Rect().Height();
	TSize minimumSize = iVersion->MinimumSize();
	TRect rect = TRect( TPoint( 0, 0 ), TSize( width, height / 4 ) );
	TPoint point = TPoint( ( rect.Width() - minimumSize.iWidth ) / 2,
		( rect.Height() - minimumSize.iHeight ) / 2 );
	iVersion->SetExtent( point, minimumSize + TSize( 0, 10 ) );
	minimumSize = iDescription->MinimumSize();
	rect = TRect( TPoint( 0, height / 4 ), TSize( width, height / 2 ) );
	point = TPoint( ( rect.Width() - minimumSize.iWidth ) / 2, rect.iTl.iY
		+ ( rect.Height() - minimumSize.iHeight ) / 2 );
	iDescription->SetExtent( point, minimumSize + TSize( 0, 10 ) );
	minimumSize = iCopyright->MinimumSize();
	rect = TRect( TPoint( 0, ( 3 * height ) / 4 ), TSize( width, height / 4 ) );
	point = TPoint( ( rect.Width() - minimumSize.iWidth ) / 2, rect.iTl.iY
		+ ( rect.Height() - minimumSize.iHeight ) / 2 );
	iCopyright->SetExtent( point, minimumSize + TSize( 0, 10 ) );
	}

/**
 *    Initialize each control upon creation.
 */
void CLDIRAbout::InitializeControlsL( TInt aWidth )
	{
	InitComponentArrayL();
	// Status texts
	// get text color from skin
	TRgb textColor;
	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
	AknsUtils::GetCachedColor( skin, textColor, KAknsIIDQsnTextColors,
		EAknsCIQsnTextColorsCG6 );
	const CFont* font = iEikonEnv->AnnotationFont();

	iVersion = new ( ELeave ) CEikLabel;
	Components().AppendLC( iVersion );
	CArrayFix<TInt>* version = new ( ELeave ) CArrayFixFlat<TInt> ( 3 );
	CleanupStack::PushL( version );
	version->AppendL( 0 ); // major
	version->AppendL( 5 ); // minor
	version->AppendL( 5 ); // build
	HBufC* text = StringLoader::LoadLC( R_VERSION_STRING, *version );
	iVersion->SetTextL( *text );
	iVersion->OverrideColorL( EColorLabelText, textColor );
	iVersion->SetFont( font );
	CleanupStack::PopAndDestroy( 2, version );
	text = NULL;
	CleanupStack::Pop( iVersion );

	iDescription = new ( ELeave ) CEikLabel;
	Components().AppendLC( iDescription );
	text = StringLoader::LoadLC( R_ABOUT_STRING );
	HBufC* wrappedText = HBufC::NewLC( text->Length() + 4 );
	CArrayFix<TInt>* lineWidths = new ( ELeave ) CArrayFixFlat<TInt> ( 3 );
	CleanupStack::PushL( lineWidths );
	lineWidths->AppendL( aWidth );
	lineWidths->AppendL( aWidth );
	lineWidths->AppendL( aWidth );
	TPtr wrappedTextPtr = wrappedText->Des();
	AknTextUtils::WrapToStringL( *text, *lineWidths, *font, wrappedTextPtr );
	iDescription->SetTextL( *wrappedText );
	iDescription->OverrideColorL( EColorLabelText, textColor );
	iDescription->SetFont( font );
	CleanupStack::PopAndDestroy( 3, text );
	text = NULL;
	CleanupStack::Pop( iDescription );

	iCopyright = new ( ELeave ) CEikLabel;
	Components().AppendLC( iCopyright );
	text = StringLoader::LoadLC( R_COPYRIGHT_STRING );
	iCopyright->SetTextL( *text );
	iCopyright->OverrideColorL( EColorLabelText, textColor );
	iCopyright->SetFont( font );
	CleanupStack::PopAndDestroy( text );
	text = NULL;
	CleanupStack::Pop( iCopyright );
	}

/** 
 * Handle global resource changes, such as scalable UI or skin events (override)
 */
void CLDIRAbout::HandleResourceChange( TInt aType )
	{
	CCoeControl::HandleResourceChange( aType );
	SetRect(
		iAvkonViewAppUi->View( TUid::Uid( ELDIRAboutViewId ) )->ClientRect() );
	}

/**
 *    Draw container contents.
 */
void CLDIRAbout::Draw( const TRect& aRect ) const
	{
	CWindowGc& gc = SystemGc();
	gc.Clear( aRect );
	MAknsSkinInstance* skin = AknsUtils::SkinInstance();
	MAknsControlContext* cc = AknsDrawUtils::ControlContext( this );
	AknsDrawUtils::Background( skin, cc, this, gc, aRect );
	}

